/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: javase - QrCode1
 * <p>Powered by webrx On 2021-12-16 15:43:13
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class QrCode1 {
    public static void main(String[] args) throws WriterException, IOException, ChecksumException, NotFoundException, FormatException {
        String qr = "c:/qr.png";

        //生成QRcode
        var bm = new QRCodeWriter().encode("http://img0.baidu.com/it/u=1119515913,3748293383&fm=26&fmt=auto", BarcodeFormat.QR_CODE, 200, 200);
        var i = MatrixToImageWriter.toBufferedImage(bm);
        ImageIO.write(i, "png", new FileOutputStream(qr));

        //读取QRcode
        var img = ImageIO.read(new File("c:/me.gif"));
        var src = new BufferedImageLuminanceSource(img);
        var binarizer = new HybridBinarizer(src);
        var binaryBitmap = new BinaryBitmap(binarizer);
        var result = new QRCodeReader().decode(binaryBitmap);
        System.out.println(result.getText());

    }
}
